/**
 * Information agent: display an information to its receivers
 * 
 * can be used to inform a group of people the meeting date
 *or to inform a person that you will be late
 *
 *@author Francis,Aidan
 */
package agent;
import java.util.Vector;
import ap.*;

public class InfoAgent implements Agent {
	private static final char FIELD_DELIM     = '|';
	private static final char VALUE_DELIM     = '=';
	
	private static final char FIELD_NAME      = 'N';
	private static final char FIELD_ADDRESS   = 'A';
	private static final char FIELD_INDEX     = 'I';
	
	private static final char FIELD_MESSAGE   = 'M';
	
	private Vector contacts;    //storage for the names of contacts (string)
	private Vector addresses;   // store the receivers' numbers	
	private String message; //a message
	private String agentString;
	private int nextAddressIndex;
	private String agentType;
	
	//constructor
    public InfoAgent()
	{
	 	this.contacts = new Vector();
		this.addresses = new Vector();
		this.nextAddressIndex = 0;
	}
    
	/**
	*give the agent the information to be sent
	*@Param message(informaton)
	*/
	public void setMessage(String m){
		this.message=m;
	}
	
    /**
	*return the information you want the agent to transfer
	*/
    public String getMessage(){		
		return this.message;
	}
	
  
	/**
	*access a contact details from the storage vector
	*based on an index
	*
	*@param n=index
	*/
	public String getContact(int n)	{
	return this.contacts.elementAt(n).toString();		
	}

	public void setType(){
		this.agentType=Agent.TYPE_INFORMATION;	
	}

	/**
	 * @return the index of the next recipient
	 */
	public int getNextIndex(){
			for(int i = 0 ; i < contacts.size();i++){
			//	if(available[i][0]==false){
					return i;
				//}
			}
			return -1;
	}
	
	/**
	 * read in all the agents values from a string
	 * the order in which they are read in is important and should 
	 * be matched with the fromString method
	 * 
	 * @param agentString
	 */
	public void fromString( String agentString ){
		String field, value;
		Tokenizer tokField = new Tokenizer(agentString, FIELD_DELIM);
		
		//first read in agent type
		try {
			this.agentType = tokField.nextToken();
		} catch (NoSuchElementException e) {
			e.printStackTrace();
		}
		//then read in all the agent values
		while( tokField.hasMoreTokens() ){			
			try {
				field = tokField.nextToken();
				value = field.substring(2);
				
				if( field.charAt(0) == FIELD_NAME ){					
					this.contacts.addElement(value);
				}
				else if( field.charAt(0) == FIELD_ADDRESS ){
					this.addresses.addElement(value);
				}
				else if( field.charAt(0) == FIELD_MESSAGE ){
					this.message = value;
				}				
				else if( field.charAt(0) == FIELD_INDEX ){
					this.nextAddressIndex = Integer.parseInt(value);
				}				

			} catch (NoSuchElementException e) {
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * the toString method writes out all the agents values 
	 * the order in which they are written out is important and should 
	 * be matched with the fromString method
	 * 
	 * @return the agent in string form
	 **/
	public String toString(){
		this.nextAddressIndex++;
	
		agentString += this.agentType;  	//	INFORMATION
		agentString += FIELD_DELIM;			//	|
	
		//write out all contact names and addresses
		for( int i=0; i<this.contacts.size(); i++ ){			
			agentString += FIELD_NAME;		//	N
			agentString += VALUE_DELIM;		//	=			
			agentString += this.contacts.elementAt(i); //name of the contact
			agentString += FIELD_DELIM;		//	|
			
			agentString += FIELD_ADDRESS;	//	A
			agentString += VALUE_DELIM;		//	=
			agentString += this.addresses.elementAt(i);//address
			agentString += FIELD_DELIM;		//	|
		}
		
		//then write out the message
		agentString += FIELD_MESSAGE;	//	M
		agentString += VALUE_DELIM;		//	=		
		agentString += message;			//  message
		agentString += FIELD_DELIM;		//	|
		
		//then write out the message
		agentString += FIELD_INDEX;		//	I
		agentString += VALUE_DELIM;		//	=		
		agentString += nextAddressIndex;//  index
		agentString += FIELD_DELIM;		//	|
		
		return agentString;
	}
	
	
	
	/**
	 * @return address is the address agent is to be sent to next
	 */
	public String getNextAddress(){
		String address;
		String token = "";
	
		address = (String)addresses.elementAt(nextAddressIndex);
		
		Tokenizer tok = new Tokenizer(address, ":");
		
		try {
			token = tok.nextToken() + ":" + tok.nextToken() + ":";
		} catch (NoSuchElementException e) {
			e.printStackTrace();
		}
		
		return token;
	}
	
	public int getNextPort(){
		String address, token, port = "";
		
		address = (String)addresses.elementAt(nextAddressIndex);
		
		Tokenizer tok = new Tokenizer(address, ":");
	
		try {
			token = tok.nextToken() + ":" + tok.nextToken() + ":";				
			port = token.substring(token.length());
		} catch (NoSuchElementException e) {
			e.printStackTrace();
		}
		
		return Integer.parseInt(port);
	}
}
